<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/1/9
 * Time: 下午1:04
 */
namespace EasySwoole;

use App\Lib\Cache\VideoList;
use App\Lib\Process\ConsumerTest;
use App\Lib\Redis\Redis;
use \EasySwoole\Core\AbstractInterface\EventInterface;
use EasySwoole\Core\Component\Crontab\CronTab;
use EasySwoole\Core\Component\Di;
use EasySwoole\Core\Component\Logger;
use EasySwoole\Core\Swoole\Process\ProcessManager;
use \EasySwoole\Core\Swoole\ServerManager;
use \EasySwoole\Core\Swoole\EventRegister;
use \EasySwoole\Core\Http\Request;
use \EasySwoole\Core\Http\Response;
use EasySwoole\Core\Swoole\Time\Timer;
use Predis;

Class EasySwooleEvent implements EventInterface {

    public static function frameInitialize(): void
    {
        // TODO: Implement frameInitialize() method.
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(ServerManager $server,EventRegister $register): void
    {
        // TODO: Implement mainServerCreate() method.
        Di::getInstance()->set('MYSQL',\MysqliDb::class,\Yaconf::get('easyswoole.mysql'));
        Di::getInstance()->set('REDIS',Redis::getInstance());

//        $allNum = 3;
//        for ($i = 0 ;$i < $allNum;$i++){
//            ProcessManager::getInstance()->addProcess("consumer_test_{$i}",ConsumerTest::class);
//        }
//        crontab定时器
//        CronTab::getInstance()
//            ->addRule('test','*/1 * * * *',function (){
//                var_dump(1);
//            })
//            ;
//        swoole定时器
//        $videoListCacheObj = new VideoList();
//        $videoListCacheObj->type = 'redis';
//        $register->add(EventRegister::onWorkerStart, function (\swoole_server $server, $workerId) use($videoListCacheObj) {
//            if ($workerId == 0) {
//                Timer::loop(10 * 1000, function () use($videoListCacheObj) {
//                    $videoListCacheObj->generation();
//                });
//            }
//        });
    }

    public static function onRequest(Request $request,Response $response): void
    {
        // TODO: Implement onRequest() method.
    }

    public static function afterAction(Request $request,Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}